({
    //查询数据
    doFetchData: function (component, event, helper) {
        //设置当前页
        component.set("v.currentPage", 1);
        helper.returnData(component);
    },
    // 上一页
    prePage: function (component, event, helper) {
        //获取当前页的值
        var currentPage = component.get("v.currentPage");
        component.set("v.currentPage", currentPage - 1);
        helper.updateCurrentPageData(component, currentPage - 1);
    },
    //下一页
    nextPage: function (component, event, helper) {
        var currentPage = component.get("v.currentPage");
        component.set("v.currentPage", currentPage + 1);
        helper.updateCurrentPageData(component, currentPage + 1);
    },

    // 选中行
    selectRow: function (cmp, event, helper) {
        console.log('selectRow...');
        //判断是否全选
        helper.checkIsAllSelected(cmp);
    },
    // 选中当前页面的所有复选框
    selectCurrPageCheckbox: function (cmp, event, helper) {
        console.log('selectCurrPageCheckbox...');
        const eSource = event.getSource();
        const isAllSelected = eSource.get('v.value');
        //拿到所有的单选框的值
        let checkboxs = cmp.find('checkboxItem');
        console.log('checkboxs:', checkboxs);
        checkboxs.forEach((checkbox) => {
            //将所有复选框的选中状态设置为 isAllSelected 的值，即全选或取消全选
            checkbox.set('v.value', isAllSelected);
        })
        
    },
    
    // 添加按钮
    addRow: function (component, event, helper) {
        console.log('addRow...');
        // 获取已经选择的数据
        let selectedIds = component.get("v.selectedRowIds");
        let selectedData = component.get("v.selectedData");
        let pageData = component.get("v.currentPageData");

        // 获取当前行的记录id
        let rowId = event.getSource().get("v.value");
        console.log('rowId:', rowId);

        //添加
        selectedIds.push(rowId);
        console.log('rowId:', rowId);
        pageData.forEach(function (item) {
            if (item.Id === rowId) {
                selectedData.push(item);
            }
        });
        component.set("v.selectedRowIds", selectedIds);
        component.set("v.selectedData", selectedData);
        //添加之后重新分页
        helper.updateCurrentPageData(component, component.get("v.currentPage"));

    },
    // 添加已勾选数据
    addSelected: function (cmp, event, helper) {
        console.log('addSelected...');
        //获取所有数据
        let allPageData = cmp.get('v.resData');
        // 获取已选中的数据(下表)
        let selectData = cmp.get("v.selectedData");
        // 遍历所有数据，将isSelected==true的数据添加到selectData中
        allPageData.forEach((item) => {
            if (item.isSelected) {
                // 重置复选框状态
                item.isSelected = false;
                // 添加数据到下表
                selectData.push(item);
            }
        });
        cmp.set('v.selectedData', selectData);
        console.log('currentPage:', cmp.get("v.currentPage"));
        //重新分页
        helper.updateCurrentPageData(cmp, cmp.get("v.currentPage"));
        //添加全部数据复选框为false
        cmp.set('v.isAllSelected',false);
       
    },
    // 转正申请
    doRegular: function (component, event, helper) {
        //把doCondition设置为doRegular
        component.set('v.doCondition', 'doRegular');
        helper.returnData(component);
        component.set('v.doCondition', 'init');
        // location.reload();
        // 将选择数据制空
        component.set("v.selectedRows", []);
        // 清除复选框勾选记录
        component.set('v.isAllSelected', false);
        var isAllSelected = component.get("v.isAllSelected");
        // 更新所有行的选中状态
        var checkboxes = component.find("checkbox");
        checkboxes.forEach(function (checkbox) {
            checkbox.set("v.value", isAllSelected);
        });
    },
    //关闭窗口
    handleClose: function (component, event, helper) {
        // window.history.back();
        window.location.href = "https://dcitcom5-dev-ed.develop.lightning.force.com/lightning/o/Employee__c/list?";
    },

    //删除
    handleDelete: function (component, event, helper) {
        var selectedMenuItemValue = event.getSource().get("v.value");
        console.log('selectedMenuItemValue=', selectedMenuItemValue)

        if (selectedMenuItemValue.includes('delete')) {
            // 执行删除逻辑
            // 提取出id值
            var selectedId = selectedMenuItemValue.replace("delete", "");
            console.log('selectedId=', selectedId);
            // 过滤掉要删除的id
            var selectedIds = component.get("v.selectedRowIds");
            console.log('seledtids:', selectedIds);
            //拿到需要的数据（过滤掉不需要的数据）
            selectedIds = selectedIds.filter(function (item) {
                return item != selectedId;
            });
            console.log('seledtids:', selectedIds);
            // 过滤掉selectedData数据
            var selectedData = component.get("v.selectedData");
            console.log('selectedData:', selectedData);
            var removedObj = null;
            selectedData = selectedData.filter(function (obj) {
                if (obj.Id === selectedId) {
                    removedObj = obj; // 保存要删除的对象
                    return false; // 过滤掉该对象
                }
                return true; // 保留其他对象
            });
            console.log('selectedData', selectedData);
            // 设置数据
            component.set("v.selectedRowIds", selectedIds);
            component.set("v.selectedData", selectedData);
            //删除之后重新分页
            helper.updateCurrentPageData(component, component.get("v.currentPage"));
        }
    }


})
